package com.enjoy.game.xcard.action;

import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.algorithm.collision.RectangularShapeCollisionChecker;
import org.andengine.util.modifier.IModifier;

import com.enjoy.game.xcard.sprite.CellLayout;
import com.enjoy.game.xcard.sprite.XCard;


public class DragController extends MovementController{
	DragContainer mDragContainer;
	
	
	public DragController(CellLayout mDestCell) {
		super(mDestCell);
		Scene scene = (Scene)mDestCell.getParent();
		mDragContainer = new DragContainer(0,0, 640, 800);
		//mDragContainer.setScale(0.8f);
		scene.attachChild(mDragContainer);
		scene.registerTouchArea(mDragContainer);
		mDragContainer.setVisible(false);
		mDragContainer.setZIndex(5);
	}


	public void triger(final XCard card, final CellLayout srcCell){
		
		if(mDestCell != null && mDestCell.acceptCard()){
			mDragContainer.StartDrag(card, srcCell);
		}
		
	}
	
	class DragContainer extends Entity implements ITouchArea{
		float mWidth, mHeight;
		XCard mCard;
		CellLayout mSrcLayout;
		int mOldSrcIndex;
		
		float[] mTmpDestCoord = new float[2];
		
		public DragContainer(float x, float y, float width, float height) {
			super(x, y);
			mWidth = width;
			mHeight = height;
		}
		
		void StartDrag(XCard card, CellLayout srcLayout){
			mCard = card;
			mSrcLayout = srcLayout;
			mOldSrcIndex = srcLayout.findEntity(card);
			card.convertLocalToSceneCoordinates(0, 0, mTmpDestCoord);
			convertSceneToLocalCoordinates(mTmpDestCoord);
			setVisible(true);
			srcLayout.removeCard(card);
			attachChild(card);
			card.setPosition(mTmpDestCoord[0], mTmpDestCoord[1]);
		}
		
		
		void finishDrag(){
			if(mCard != null){
				detachChild(mCard);
				setVisible(false);
				mCard = null;
			}
		}
		
		@Override
		public boolean contains(float pX, float pY) {
			return RectangularShapeCollisionChecker.checkContains(this, mWidth, 
					mHeight, pX, pY);
		}

		@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
			boolean handled = false;
			
			switch (pSceneTouchEvent.getAction()) {
			case TouchEvent.ACTION_DOWN:

				break;
				
			case TouchEvent.ACTION_MOVE:
				if(mCard != null){
					mCard.setPosition(pTouchAreaLocalX, pTouchAreaLocalY);
					handled = true;
				}
				break;
				
			case TouchEvent.ACTION_UP:
				if(mCard != null){
					XCard card = mCard;
					finishDrag();
					convertLocalToSceneCoordinates(pTouchAreaLocalX, pTouchAreaLocalY, mTmpDestCoord);
					mDestCell.convertSceneToLocalCoordinates(mTmpDestCoord);
					boolean success = mDestCell.putCard(card, mTmpDestCoord[0], mTmpDestCoord[1] );
					if(!success){
						mSrcLayout.putCard(card, mOldSrcIndex);
					}
					handled = true;
				}
	
				break;
			}
			return handled;
		}
		
	}

}
